package com.fourforfo.fourmall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: xxl-sso-core
 * @description: 自定义的延迟消息队列配置，一旦创建好后，重启该服务不会再次创建
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-09-09 16:19
 **/
@Configuration
public class MyOrderMQConfig {

    //延迟队列名
    public static String ORDER_DELAY_QUEUE="order.delay.queue";
    //订单释放队列名
    public static String ORDER_RELEASE_QUEUE="order.release.queue";
    //交换机名字
    public static String ORDER_EVENT_EXCHANGE="order-event-exchange";
    //订单延迟路由
    public static String ORDER_DELAY_ROUTE="order.create.order";
    //订单释放路由
    public static String ORDER_RELEASE_ROUTE ="order.release.order";

    //秒杀商品自动创建 队列
    public static String SECKILL_ORDER_CREATE_QUEUE="order.seckill.order.queue";

    //秒杀商品自动创建队列的路由
    public static String SECKILL_ORDER_CREATE_ROUTE="order.seckill.order";


    /**
     * 带过期时间的队列，等时间一到，就将消息转发到死信队列中
     *  不能被任何服务监听，由消息自动过期
     * @return
     */
    @Bean
    public Queue orderDelayQueue(){
        //String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        /*
        arguments:
            x-dead-letter-exchange: order-event-exchange
            x-dead-letter-routing-key: order.release.order
            x-message-ttl: 60000
         */
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","order-event-exchange");
        arguments.put("x-dead-letter-routing-key","order.release.order");
        arguments.put("x-message-ttl",60000);//ttl以毫秒为单位，此处表示60s
        return new Queue(ORDER_DELAY_QUEUE,true,false,false,arguments);
    }

    /**
     * 延迟队列
     * @return
     */
    @Bean
    public Queue orderReleaseQueue(){
        //设置延迟队列为order.release.queue
        return new Queue(ORDER_RELEASE_QUEUE,true,false,false);
    }

    /**
     * 交换机，根据路由确定消息转发到哪个队列
     * @return
     */
    @Bean
    public Exchange orderEventExchange(){
        //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
        return new TopicExchange(ORDER_EVENT_EXCHANGE,true,false);
    }

    /**
     * 秒杀订单创建queue
     * @return
     */
    @Bean
    public Queue seckillOrderCreateQueue(){
        //设置延迟队列为order.seckill.order.queue
        return new Queue(SECKILL_ORDER_CREATE_QUEUE,true,false,false);
    }

    /**
     * 秒杀订单创建binding
     * @return
     */
    @Bean
    public Binding seckillOrderCreateBinding(){

        return new Binding(SECKILL_ORDER_CREATE_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                SECKILL_ORDER_CREATE_ROUTE,
                null);
    }

    @Bean
    public Binding orderCreateBinding(){
        /**
         * String destination,
         * DestinationType destinationType,
         * String exchange,
         * String routingKey,
         * Map<String, Object> arguments
         */
        return new Binding(ORDER_DELAY_QUEUE,Binding.DestinationType.QUEUE,ORDER_EVENT_EXCHANGE,ORDER_DELAY_ROUTE,null);
    }

    @Bean
    public Binding orderReleaseBinding(){
        return new Binding(ORDER_RELEASE_QUEUE,Binding.DestinationType.QUEUE,ORDER_EVENT_EXCHANGE, ORDER_RELEASE_ROUTE,null);
    }

    /**
     * 订单释放直接和库存解锁进行绑定
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.queue",//与库存释放队列绑定
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE,
                "order.release.other.#"//库存释放队列的路由键
                ,null);
    }
}
